-
Notifications
You must be signed in to change notification settings - Fork 350
Java Extension Optimizations #835
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Java Extension Optimizations #835
Conversation
java/src/json/ext/LinkedSegmentedByteListDirectOutputStream.java
Outdated
Show resolved
Hide resolved
private static final int DEFAULT_CAPACITY = 1024; | ||
|
||
private int totalLength; | ||
private byte[][] segments = new byte[21][]; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why 21
? The minimum segment size is 1024
for the first segment. The code doubles the segment size for each additional segment. Based on this doubling, we only need 21 segments before we hit Integer.MAX_VALUE
.
…SegmentedByteListDirectOutputStream.
…ing the output buffer.
Synthetic benchmarks of encoding an array of 128-byte ASCII strings.
SegmetedByteListDirectOutputStream + SWAR
ByteListDirectOutputStream + Scalar (effectively the same code as master)
SegmentedByteListDirectOutputStream + Scalar
ByteListDirectOutputStream + SWAR
Master
|
java/src/json/ext/StringEncoder.java
Outdated
|
||
if (pos + 4 <= len) { | ||
int x = bb.getInt(ptr + pos); | ||
int is_ascii = 0x808080 & ~x; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This hex number only checks 3 bytes.
Maybe 0x808080
→ 0x80808080
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Great catch, thank you! Late night coding without my glasses...
Interestingly no spec failed. I'll try to address that.
Changelog 📓
OutputStream
to reduceSystem.arraycopy
's each time the output buffer is resized.StringEncoder#encode
to include a SWAR-based fast path for basic JSON encoding. The algorithm is from this post. It's the same as the vector-based algorithm in the C extension.These features can be toggled with the system properties
json.useSegmentedOutputStream
andjson.useSWARBasicEncoder
. Both default totrue
. I'm happy to remove these. They made testing and benchmarking much easier.Benchmarks
SegmentedByteListDirectOutputStream + SWAR
ByteListDirectOutputStream + SWAR
ByteListDirectOutputStream + Scalar
SegmentedByteListDirectOutputStream + Scalar
master (as of commit 37e6890)